{extend name="base"/}
{block name="resources"}
{/block}
{block name="main"}
<div class="layui-collapse ns-tips">
    <div class="layui-colla-item">
        <h2 class="layui-colla-title">操作提示</h2>
        <ul class="layui-colla-content layui-show">
           <!-- <li>您可以通过选择所属广告位来获取该广告位下的所有广告</li>-->
        </ul>
    </div>
</div>

<div class="ns-single-filter-box">
    <button class="layui-btn ns-bg-color" onclick="add()">添加导航</button>

    <div class="layui-form">

        <div class="layui-input-inline ns-len-mid">
            <input type="text" name="search_text" placeholder="请输入导航名称" class="layui-input">
            <button type="button" class="layui-btn layui-btn-primary" lay-filter="search" lay-submit>
                <i class="layui-icon">&#xe615;</i>
            </button>
        </div>
    </div>


</div>

<!-- 列表 -->
<table id="nav_list" lay-filter="nav_list"></table>

<!-- 广告图片 -->
<script type="text/html" id="nav_image">
    <div class="ns-img-box">
        <img layer-src src="{{ns.img(d.image)}}" />
    </div>
</script>

<!-- 排序 -->
<script type="text/html" id="slide_sort">
    <input name="slide_sort" type="number" onchange="editSort({{d.nav_id}},'slide_sort', this)" value="{{d.slide_sort}}" class="layui-input ns-sort-len">
</script>

<!-- 广告链接 -->
<script type="text/html" id="nav_url">
	<a href="{{d.nav_url}}">{{d.nav_url}}</a>
</script>

<!-- 操作 -->
<script type="text/html" id="action">
    <div class="ns-table-btn">
        <a class="layui-btn" lay-event="edit">编辑</a>
        <a class="layui-btn" lay-event="delete">删除</a>
    </div>
</script>

<!-- 批量删除 -->
<script type="text/html" id="toolbarOperation">
    <button class="layui-btn layui-btn-primary" lay-event="del">批量删除</button>
</script>
<!-- 批量删除 -->
<script type="text/html" id="batchOperation">
    <button class="layui-btn layui-btn-primary" lay-event="del">批量删除</button>
</script>
{/block}
{block name="script"}
<script>
    layui.use('form', function () {
        var table,
            form = layui.form,
            repeat_flag = false; //防重复标识
        form.render();

        table = new Table({
            elem: '#nav_list',
            url: ns.url("admin/nav/lists"),
            where: {

            },
            cols: [
                [{
                    width: '3%',
                    type: 'checkbox',
                    unresize: 'false',
                }, {
                    field: 'nav_title',
                    title: '导航名称',
                    unresize: 'false',
                    width: '15%'
                }, /*{
                    title: '导航链接',
                    unresize: 'false',
                    width: '28%',
					templet: function(data) {
                    	var nav_url = data.nav_url;
                    	if(nav_url){
                            nav_url = JSON.parse(data.nav_url);
							return nav_url.title ? nav_url.title : '';
                        }else{
                    		return '';
                        }
					}
                },*/ {
                    title: '导航图片',
                    unresize: 'false',
                    templet: '#nav_image',
                    width: '15%'
                }, {
                    title: '排序',
                    unresize: 'false',
                    templet: '#slide_sort',
                    width: '12%'
                }, {
                    title: '操作',
                    toolbar: '#action',
                    unresize: 'false',
                    width: '12%'
                }]
            ],
            bottomToolbar: "#batchOperation",
            toolbar: "#toolbarOperation"
        });

        /**
         * 监听工具栏操作
         */
        table.tool(function(obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit': //编辑
                    location.href = ns.url("admin/nav/editnav",{"nav_id":data.nav_id});
                    break;
                case 'delete': //删除
                    deleteNav(data.nav_id);
                    break;
            }
        });

        /**
         * 批量操作
         */
        table.bottomToolbar(function(obj) {
            if (obj.data.length < 1) {
                layer.msg('请选择要操作的数据');
                return;
            }

            switch (obj.event) {
                case "del":
                    var id_array = new Array();
                    for (i in obj.data) id_array.push(obj.data[i].nav_id);
                    deleteNav(id_array.toString());
                    break;
            }
        });

        /**
         * 批量操作
         */
        table.toolbar(function(obj) {
            if (obj.data.length < 1) {
                layer.msg('请选择要操作的数据');
                return;
            }

            switch (obj.event) {
                case "del":
                    var id_array = new Array();
                    for (i in obj.data) id_array.push(obj.data[i].nav_id);
                    deleteNav(id_array.toString());
                    break;
            }
        });

        /**
         * 删除
         */
        function deleteNav(nav_ids) {
            if (repeat_flag) return false;
            repeat_flag = true;

            layer.confirm('确定要删除该导航吗?', function() {
                $.ajax({
                    url: ns.url("admin/nav/deleteNav"),
                    data: {'nav_ids': nav_ids},
                    dataType: 'JSON',
                    type: 'POST',
                    success: function(res) {
                        layer.msg(res.message);
                        repeat_flag = false;

                        if (res.code == 0) {
                            table.reload();
                        }
                    }
                });
            }, function () {
                layer.close();
                repeat_flag = false;
            });
        }

        /**
         * 监听搜索
         */
        form.on('submit(search)', function(data){
            table.reload({
                page: {
                    curr: 1
                },
                where: data.field
            });
        });
    });

    // 监听单元格编辑
    function editSort(nav_id, type, event){
        var value = $(event).val();

        if(!new RegExp("^-?[1-9]\\d*$").test(value)){
            layer.msg("排序号只能是整数");
            return ;
        }
        if(value<0){
			layer.msg("排序号必须大于0");
			return ;
        }

        $.ajax({
            type: 'POST',
            url: ns.url("admin/nav/editNavField"),
            data: {'type': type, 'value': value, 'nav_id': nav_id},
            dataType: 'JSON',
            success: function(res) {
                layer.msg(res.message);
                if(res.code==0){
                    table.reload();
                }
            }
        });
    }

    function add() {
        location.href = ns.url("admin/nav/addNav");
    }
</script>
{/block}